package com.ctg.itrdc.imageconsole.control.deploy;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ctg.itrdc.imageconsole.common.bean.CommonResult;
import com.ctg.itrdc.imageconsole.common.bean.ReturnCodeDefine;
import com.ctg.itrdc.imageconsole.common.util.TimeUtils;
import com.ctg.itrdc.imageconsole.data.dto.DMSGroupAndNodeDTO;
import com.ctg.itrdc.imageconsole.data.dto.DMSParamGroupDTO;
import com.ctg.itrdc.imageconsole.data.dto.DMSReturnGroupDTO;
import com.ctg.itrdc.imageconsole.service.deploy.GroupManager;
import com.ctg.itrdc.pasp.security.utils.WebUtils;
import com.ctg.itrdc.paspsdk.common.entity.PageInfo;

/*
 * 			Author			Date			Operation
 * 			Shizhu.Peng		2017-03-06		create
 */

@Controller
@RequestMapping("/dms")
public class GroupController {
	
	private static Logger logger = LoggerFactory.getLogger(GroupController.class);
	
	@Resource
	private GroupManager groupManager;
	
	
	
	@RequestMapping(value = "/group/create", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult addGroup(@RequestBody DMSParamGroupDTO dto)
	{					
		try
		{
			dto.setCreateUserId(WebUtils.getCtgUser().getSysUserId());			
			dto.setCreateTime(TimeUtils.getNowDateStr());
			dto.setLastUpdateUserId(WebUtils.getCtgUser().getSysUserId());
			dto.setLastUpdateTime(TimeUtils.getNowDateStr());
			if(null == dto.getGroupCode() || null == dto.getK8sCode() || null == dto.getNodeList())
				throw new RuntimeException("Error : some parameters are null, check them");
			if(groupManager.groupExist(dto.getK8sCode(), dto.getGroupCode()))
				throw new RuntimeException("Group Exists");
			groupManager.addGroup(dto);
			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, null);
		} catch(Exception e) {
			logger.error(e.getMessage());
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage(), null); 
		}		
	}
	
	@RequestMapping(value = "/group/delete", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult deleteGroup(@RequestBody DMSParamGroupDTO dto)
	{				
		try
		{			
			if(null == dto.getId())
				throw new RuntimeException("Error : some parameters are null, check them");
			groupManager.deleteGroup(dto.getId());
			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, null);
		} catch(Exception e) {
			logger.error(e.getMessage());
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage(), null); 
		}		
	}
	
	@RequestMapping(value = "/group/update", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult updateGroup(@RequestBody DMSParamGroupDTO dto)
	{		
		try
		{			
			dto.setLastUpdateUserId(WebUtils.getCtgUser().getSysUserId());			
			dto.setLastUpdateTime(TimeUtils.getNowDateStr());
			if(null == dto.getGroupCode() || null == dto.getId() || null == dto.getNodeList())
				throw new RuntimeException("Error : some parameters are null, check them");
			groupManager.updateGroup(dto.getId(), dto.getGroupCode(), dto.getNodeList());
			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, null);
		} catch(Exception e) {
			logger.error(e.getMessage());
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage(), null); 
		}		
	}
	
	
	/* ---- GET ---- */
	
	
	@RequestMapping(value = "/group/list", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult getGroups(@RequestParam(value = "k8sCode") String k8sCode,
			@RequestParam(value = "groupCode", required = false) String groupCode,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize)				
	{		
		if(null == pageNum)
			pageNum = new Integer(1);
		if(null == pageSize)
			pageSize = new Integer(12);
		try
		{
			PageInfo<DMSReturnGroupDTO> pageInfo = groupManager.getGroups(k8sCode, groupCode, pageNum, pageSize);
			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, pageInfo);
		} catch(Exception e) {
			logger.error(e.getMessage());
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage(), null); 
		}		
	}
	
	@RequestMapping(value = "/group/list1", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult getGroupsAndItsNodes(@RequestParam(value = "k8sCode") String k8sCode,
			@RequestParam(value = "groupCode", required = false) String groupCode,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize)				
	{		
		if(null == pageNum)
			pageNum = new Integer(1);
		if(null == pageSize)
			pageSize = new Integer(12);
		try
		{
			PageInfo<DMSGroupAndNodeDTO> pageInfo = groupManager.getGroupsAndItsNodes(k8sCode, groupCode, pageNum, pageSize);
			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, pageInfo);
		} catch(Exception e) {
			logger.error(e.getMessage());
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage(), null); 
		}		
	}	
	
}

